import {app,BrowserWindow,ipcMain, dialog } from 'electron'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'

const __dirname = dirname(fileURLToPath(import.meta.url))

const createWindow = ()=>{
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        autoHideMenuBar: true,
        title: 'live辅助安装器',
        // frame: false,
        // icon: '../public/vite.svg',
        webPreferences: {
            preload: path.join(__dirname,'./preload/preload.js'),
        }
    })

    win.loadFile('./electron/src/index.html')
}

app.whenReady().then(()=>{
    createWindow()
})

// 处理文件选择请求
ipcMain.handle('open-file-dialog', async () => {
    const result = await dialog.showOpenDialog({
        properties: ['openFile'],
        title: '选择文件',
        filters: [
        { name: '所有文件', extensions: ['*'] }
        ]
    });

    if (!result.canceled && result.filePaths.length > 0) {
        return result.filePaths[0]; // 返回完整文件路径
    }
    return null;
});

// 处理文件夹路径提取
ipcMain.handle('get-folder-path', async (event, filePath) => {
    return path.dirname(filePath);
});


import AdmZip from '@zwg/adm-zip'
import fs from 'fs'
import axios from 'axios'


// 确保目标目录存在
function ensureDirectoryExistence(dirPath) {
    if (!fs.existsSync(dirPath)) {
        fs.mkdirSync(dirPath, { recursive: true });
    }
}

ipcMain.handle('downloadAndUnzip', async ()=>{

    // const lanZouYun = 'https://milianlloveyou.lanzoue.com/ii6L32yq6g5a'
    
    // const jieXiWangZhi = 'https://www.hhlqilongzhu.cn/api/lanzouyun_jx.php'

    // try{
    //     const jieXiResponse = await axios({
    //         method: 'GET',
    //         url: jieXiWangZhi,
    //         params:{
    //             url: lanZouYun,
    //             t: Date.now()
    //         },
    //         responseType: 'json'
    //     })
    //     console.log(jieXiResponse.data);
    //     if(jieXiResponse.data.code === 200){
    //         const downUrl = jieXiResponse.data.downUrl
    //         const fileName = jieXiResponse.data.name
    //         const outputDir = './download'; // 指定解压目录

    //         try {
    //             // 1. 下载压缩包
    //             console.log('正在下载...');
    //             const response = await axios({
    //                 method: 'GET',
    //                 url: downUrl,
    //                 maxRedirects: 5, 
    //                 responseType: 'arraybuffer', 
    //                 timeout: 10000,
    //                 // 添加下载进度
    //                 onDownloadProgress: (progressEvent) => {
    //                     const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total);
    //                     console.log(`下载进度: ${percent}%`);
    //                 },
    //             });

    //             // 2. 确保输出目录存在
    //             ensureDirectoryExistence(outputDir);

    //             fs.writeFileSync(path.join(outputDir,fileName),Buffer.from(response.data))

    //         } catch (error) {
    //             console.error('操作失败：', error.message);
    //         }
    //     }
        
    // }catch{

    // }

    // const zip = new AdmZip('./download/ZortMenu v1.1.zip')
    // zip.extractAllTo('./output',true)

    // console.log('解压完成');

    const response = await axios({
        method: 'GET',
        url: 'https://gitee.com/milian01/assistant-installer/raw/main/electron/data/data.json',
        responseType: 'json'
    })

    console.log(response);
})

